from tel_manage import *


class Ui(object):
    '''初始化，显示欢迎界面'''

    def __init__(self):
        print(
            "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * "
        )
        print(
            "*                                                                 *"
        )
        print("*                        电话簿管理系统V0.1                       *")
        print("*                         作者:超级洗面奶                         *")
        print(
            "*                                                                 *"
        )
        print(
            "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * "
        )
        print("第一次使用，可以输入help命令获取帮助。")

    '''显示帮助菜单'''

    def show_menu(self, menus, commands):
        for i in range(0, len(menus)):
            print(menus[i] + "(" + commands[i] + ")")
        print("请输入对应的命令。")

    '''获取操作命令和参数'''

    def get_command(self, commands):
        while (True):
            command = input(">").strip().lower()
            command_info = command.split(" ")
            command_style = command_info[0]
            if command_style in commands:
                command_content = command.replace(command_style, "").lstrip()
                return (command_style, command_content)
            else:
                print("您输入的命令不正确，请重新输入。")

    '''显示列表头'''

    def show_tel_header(self, header):
        print("{0}\t{1}\t{2}\t{3}".format(header[0], header[1], header[2],
                                          header[3]))

    '''显示列表内容'''

    def show_tel_book(self, tel_book):
        for i in range(0, len(tel_book)):
            print("{0}\t{1}\t{2}\t{3}".format(i+1, tel_book[i][0],
                                              tel_book[i][1],
                                              tel_book[i][2].replace("\n",
                                                                     "")))

    def choose_operate_object(self, tel_data, header):
        name = input("请输入联系人姓名：")
        if name == "exit":
            return
        edit_info = tel_data.search(name)
        while edit_info == "":
            print("查无此人，请重新输入")
            name = input("请输入联系人姓名：")
            if name == "exit":
                return
            edit_info = tel_data.search(name)
        self.show_tel_header(header)
        self.show_tel_book(edit_info)
        i = int(input("请选择序号："))
        while i < 1 or i > len(edit_info):
            i = int(input("请选择序号："))
        return edit_info[i - 1]

    def edit(self, operate_object):
        if (operate_object is None):#判断返回对象是否为空
            return
        name = input("请输入姓名(不修改直接按回车键)：")
        if name != "":
            operate_object[0] = name
        tel_number = input("请输入电话号码(不修改直接按回车键)：")
        if tel_number != "":
            operate_object[1] = tel_number
        tel_address = input("请输入住址(不修改直接按回车键)：")
        if tel_address != "":
            operate_object[2] = tel_address

    def new(self):
        name = input("请输入姓名：")
        operate_object = []
        if name != "":
            operate_object.append(name) 
        tel_number = input("请输入电话号码：")
        if tel_number != "":
            operate_object.append(tel_number) 
        tel_address = input("请输入住址：")
        if tel_address != "":
            operate_object.append(tel_address)
        return operate_object

    def delete(self, tel_book, operate_object):
        tel_book.remove(operate_object)

    def confirm_exit(self):
        if (input("退出前请确认已使用save命令保存。\n确认退出？（Y/N）").lower() == "y"):
            return True
        else:
            return False
